package com.liyujie.networkrequest.ui.home;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.lifecycle.ViewModelProvider;

import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ScreenUtils;
import com.liyujie.networkrequest.R;
import com.liyujie.networkrequest.common.Constants;
import com.liyujie.networkrequest.databinding.ActivityHomeBinding;
import com.liyujie.networkrequest.mobile.event.MessageEvent;
import com.liyujie.networkrequest.ui.BaseActivity;
import com.liyujie.networkrequest.utils.CommonUtils;
import com.liyujie.networkrequest.utils.open.OpenCabinetUtils;
import com.liyujie.networkrequest.utils.serialport.OpenPortUtils;
import com.liyujie.networkrequest.views.NoDoubleClickListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;

/**
 * 首页
 */
public class HomeActivity extends BaseActivity<ActivityHomeBinding> {

    /**
     * VM
     */
    private HomeViewModel homeViewModel;


    @Override
    public void initCreate() {
        super.initCreate();
        //注册，重复注册会导致崩溃
        EventBus.getDefault().register(this);
    }


    @Override
    public int initContentView(Bundle savedInstanceState) {

        return R.layout.activity_home;
    }

    @Override
    public void initViewModel() {
        super.initViewModel();
        //VM
        homeViewModel = new ViewModelProvider(getViewModelStore(), new ViewModelProvider.AndroidViewModelFactory(getApplication())).get(HomeViewModel.class);
        binding.setViewModel(homeViewModel);

    }



    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onMessageEvent(MessageEvent mMessageEvent) {
        switch (mMessageEvent.getType()){
            case SendFeedback://发送反馈
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        HashMap<Integer, Long> receivedMap = mMessageEvent.getSerializable();
                        for (Map.Entry<Integer, Long> entry : receivedMap.entrySet()) {
                            try {
                                /**
                                 * 开门门号
                                 */
                                Integer key = entry.getKey();
                                /**
                                 * 开门时间
                                 */
                                Long value = entry.getValue();
                                /**
                                 * 关门消息推送接口
                                 */
                                OpenCabinetUtils.closeBox(key,value);
                                /**
                                 * 延迟1秒
                                 */
                                Thread.sleep(1000);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }).start();
                break;
        }
    }




    @Override
    public void initViewObservable() {
        super.initViewObservable();
        //后台管理
        View headView = binding.navView.getHeaderView(0);
        LinearLayout llDistance = headView.findViewById(R.id.ll_distance);
        ViewGroup.LayoutParams params =  llDistance.getLayoutParams();
        params.height = ScreenUtils.getScreenHeight() - 100;
        llDistance.setLayoutParams(params);
        /**
         * 后台管理
         */
        headView.findViewById(R.id.BackgroundManagement).setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if(null != homeViewModel){
                    homeViewModel.showDialog();
                    binding.Drawer.closeDrawers();
                }
            }
        });
        //版本
        TextView tv_edition = headView.findViewById(R.id.tv_edition);
        tv_edition.setText("V:"+ AppUtils.getAppVersionName());
        /**
         * 定时器获取反馈
         */
        TimerAcquisitionFeedback();
    }


    /**
     * 定时器获取反馈
     */
    private void TimerAcquisitionFeedback() {
        Observable.interval(15, TimeUnit.SECONDS).subscribe(new io.reactivex.Observer<Long>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(Long aLong) {
                /**
                 * 正在获取反馈
                 */
                Constants.BeBeingFeedback = true;
                /**
                 * 调用串口反馈
                 */
                CommonUtils.AcquireFeedbackData();
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });

    }


    @Override
    protected void onDestroy() {
        //解绑
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }
}
